//=============================================================================

#include "../../../../inc/aJoint.h"

//=============================================================================

aJoint::aJoint()
{
  memset(bindMatrix, 0, sizeof(float)*MATRIX_SIZE);
  memset(currentMatrix, 0, sizeof(float)*MATRIX_SIZE);
  parent = 0;
  weights = 0;
  name = "defaultJoint";
  vertexesCount = 0;
}

//=============================================================================

aItem<aAnimationsTrack>* aJoint::ajGetAnimationByName(const aString& name) const
{
  if (name.asGetLength() <= 0)
    return 0;
  
  aItem<aAnimationsTrack>* tmpAnim = track.alGetFirstItem();

  while(tmpAnim)
  {
    if (tmpAnim->data.caption == name)
      return tmpAnim;

    tmpAnim = tmpAnim->next;
  }

  return 0;
}

//=============================================================================

void aJoint::ajCopyDataToCurrentMatrix()
{
    currentMatrix[0] = ijk[0].x;
    currentMatrix[1] = ijk[0].y;
    currentMatrix[2] = ijk[0].z;
    currentMatrix[4] = ijk[1].x;
    currentMatrix[5] = ijk[1].y;
    currentMatrix[6] = ijk[1].z;
    currentMatrix[8] = ijk[2].x;
    currentMatrix[9] = ijk[2].y;
    currentMatrix[10] = ijk[2].z;
    currentMatrix[12] = position.x;
    currentMatrix[13] = position.y;
    currentMatrix[14] = position.z;
}

//=============================================================================
//Clone everything, but parent & childs. Look for aSkeleton::asCloneSkeleton

void aJoint::ajCloneJoint(const aJoint& joint)
{
  child.alRemoveItems();

  parent = 0;
  name = joint.name;
  memcpy(bindMatrix, joint.bindMatrix, sizeof(float)*MATRIX_SIZE);
  memcpy(currentMatrix, joint.currentMatrix, sizeof(float)*MATRIX_SIZE);
  bindPosition = joint.bindPosition;
  bindIJK[0] = joint.bindIJK[0];
  bindIJK[1] = joint.bindIJK[1];
  bindIJK[2] = joint.bindIJK[2];
  position = joint.position;
  ijk[0] = joint.ijk[0];
  ijk[1] = joint.ijk[1];
  ijk[2] = joint.ijk[2];
  bindIJKValue[0] = joint.bindIJKValue[0];
  bindIJKValue[1] = joint.bindIJKValue[1];
  bindIJKValue[2] = joint.bindIJKValue[2];
  memcpy(weights, joint.weights, sizeof(float)*vertexesCount);

  joint.track.alCopyItems(track);
}

//=============================================================================

aJoint::~aJoint()
{
  if (weights)
    delete [] weights;
}

//=============================================================================
